package com.tuanzi.loan.web.bean.family;

import com.tuanzi.loan.business.entity.onekey.CtrlOneKeyOnline;
import com.tuanzi.loan.business.service.onekey.CtrlOneKeyOnlineService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.server.loan_base.constant.onekey.CtrlOneKeyOnlineConfig;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.Calendar;

/**
 * @author caiwanlin
 * @create 2018-07-12 14:53
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CtrlOneKeyOnlineInfoBean extends JSFInfoBean<CtrlOneKeyOnline> {
    @Autowired
    private CtrlOneKeyOnlineService ctrlOneKeyOnlineService;

    @PostConstruct
    public void init(){
        String id = getParamFromRequest("id");
        if (id != null){
            $ = ctrlOneKeyOnlineService.findOne(Long.parseLong(id));
        }
        else {
            $ = new CtrlOneKeyOnline();
            changeStatusToAdd();
        }
    }

    public void onSave(){
        Long id = $.getId();
        if (id != null){
            ctrlOneKeyOnlineService.saveOrUpdate($);
            changeStatusToView();
            addSuccessfullyMessage();
        }
        else {
            $ = ctrlOneKeyOnlineService.saveOrUpdate($);
            changeStatusToView();
            addSuccessfullyMessage();
        }
    }

    public void online(Long id){
        ctrlOneKeyOnlineService.online(id);
        $.setStatus(CtrlOneKeyOnlineConfig.Status.ONLINE.index);
        addSuccessfullyMessage();
    }

    public void offline(Long id){
        ctrlOneKeyOnlineService.offline(id);
        $.setStatus(CtrlOneKeyOnlineConfig.Status.OFFLINE.index);
        addSuccessfullyMessage();
    }
}
